package cn.xyinping.hrm.service.impl;

import cn.xyinping.hrm.constans.BasicConstans;
import cn.xyinping.hrm.constans.ErrorCode;
import cn.xyinping.hrm.domain.Employee;
import cn.xyinping.hrm.domain.Login;
import cn.xyinping.hrm.domain.Tenant;
import cn.xyinping.hrm.dto.LoginMealDto;
import cn.xyinping.hrm.dto.TenantRegisterDto;
import cn.xyinping.hrm.feign.LoginFeign;
import cn.xyinping.hrm.mapper.TenantMapper;
import cn.xyinping.hrm.service.IEmployeeService;
import cn.xyinping.hrm.service.ITenantService;
import cn.xyinping.hrm.util.AjaxResult;
import cn.xyinping.hrm.util.AssertUtil;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xyinping
 * @since 2021-11-20
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {
    @Autowired
    private LoginFeign loginFeign;
    @Autowired
    private IEmployeeService employeeService;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void entering(TenantRegisterDto dto) { // @Valid 判断对象非空
        // 1. 校验参数非空
        Employee employee = dto.getEmployee();
        Login login = dto.getLogin();
        Tenant tenant = dto.getTenant(); // 机构
        Long mealId = dto.getMealId();
        // 用户名
        AssertUtil.isNotEmpty(login.getUsername(), ErrorCode.LOGIN_USERNAME_IS_NULL.getFullMessage());
        // 密码
        AssertUtil.isNotEmpty(login.getPassword(), ErrorCode.LOGIN_PASSWORD_IS_NULL.getFullMessage());
        // 机构
        AssertUtil.isNotEmpty(tenant.getCompanyName(), ErrorCode.LOGIN_TENANT_IS_NULL.getFullMessage());
        // 管理员电话
        AssertUtil.isNotEmpty(employee.getTel(), ErrorCode.LOGIN_PHONE_IS_NULL.getFullMessage());
        // 管理员邮箱
        AssertUtil.isNotEmpty(employee.getEmail(), ErrorCode.LOGIN_EMAIL_IS_NULL.getFullMessage());
        // 2. 保存login表，密码加密
        // 3. 保存机构和套餐关系表,login和套餐中间表；
        LoginMealDto loginMeal = new LoginMealDto();
        loginMeal.setLogin(login);
        loginMeal.setMealId(mealId);
        AjaxResult ajaxResult = loginFeign.saveLoginAndMeal(loginMeal);
        if(!ajaxResult.isSuccess()||ajaxResult.getResultObj()==null){
            throw new RuntimeException("用户登陆失败");
        }
        // 登陆用户Id
        Long loginId = Long.valueOf(ajaxResult.getResultObj().toString());
        //  4.保存管理员(employee) ，employee关联login的Id
        Employee employee1 = new Employee();
        employee1.setEmail(employee.getEmail());
        employee1.setTel(employee.getTel());
        // 登陆用户Id
        employee1.setLoginId(loginId);
        // 注册时间
        Date date = new Date();
        employee1.setInputTime(date);
        // 机构管理员
        employee1.setType(BasicConstans.Employee.TYPE_TENANT_ADMIN);
        employeeService.insert(employee1);
        // 5.保存机构：机构关联管理员ID
        tenant.setRegisterTime(date);
        // 审核状态 待审核
        tenant.setState(BasicConstans.Tenant.STATE_WAIT_AUDIT);
        tenant.setAdminId(employee1.getId());
        insert(tenant);

    }
}
